package com.example.jdbc.simple;

import java.sql.*;

/**
* @Description: 查询用户信息
* @Param: 
* @return: 
* @Author: tangzhaofeng
* @Date: 5/13/2024
*/
public class UserDao {

    private final static String URL = "jdbc:mysql://8.134.212.67:3688/paportal?useUnicode=true&allowMultiQueries=true&characterEncoding=utf8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai&useSSL=false";

    private final static String USERNAME = "root";

    private final static String PASSWORD = "88888888@";

    public static void main(String[] args) throws SQLException {

        Connection connection = null;

        PreparedStatement preparedStatement = null;

        try {
            //加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //通过数据库获取连接
            connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
            // 定义SQL 语句
            String querySQL = "select * from sys_user where user_name = ?";
            // 预处理SQL
            preparedStatement = connection.prepareStatement(querySQL);
            // 给SQL设置参数
            preparedStatement.setString(1,"test");
            //处理完SQL了，发送给Mysql服务进行查询
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                System.out.println(resultSet.getString("user_name"));
            }

        } catch (ClassNotFoundException | SQLException e) {
            throw new RuntimeException(e);
        }finally {
            connection.close();
            preparedStatement.close();
        }
    }

}
